package activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author lishaojun
 * @create 2019-07-08-下午 5:31
 */
public class JmsConsumer_Topic_Persist {
    public static final String ACTIVEMQ_URL = "tcp://106.13.85.181:61616";
    public static final String TOPIC_NAME = "topic-persist";
    public static void main(String args[]) throws Exception{
        System.out.println("z3");
        //1.创建连接工厂,按照地址给定的用户名密码
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2.通过连接工厂获得连接
        Connection connection=activeMQConnectionFactory.createConnection();
        connection.setClientID("z3");
        //3.创建会话,方法两个参数一个是事务，一个是签收
        Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4.创建目的地，具体是队列或者是主题
        Topic topic=session.createTopic(TOPIC_NAME);
        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "remark...");
        //5.创建消息的消费者
        MessageConsumer messageConsumer=session.createConsumer(topic);

        connection.start();
        Message message = topicSubscriber.receive();
        while (null!=message){
            TextMessage textMessage= (TextMessage) message;
            System.out.println("******收到的持久化："+textMessage.getText());
            message=topicSubscriber.receive();//30秒之后取关

        }
        session.close();
        connection.close();
        System.out.println("主题接收完毕");
    }
}
